/**
 * 用户管理相关逻辑
 * @type {*}
 */
var pageObj = $('#page');
var pageSize = 50;
var cPage = 1;

init();

//初始化
function init() {
    // page, pageSize
    loadData(cPage);
}

function processPage(data) {
    // 处理分页
    pageObj.children().remove();
    pageObj.append('<div id="create_page_num"></div>');
    pageObj.find("#create_page_num").createPage({
        pageCount: data.pages,
        current: data.pageNum,
        backFn: function (p) {
            loadData(p, pageSize);
        }
    });
}

// 加载分页列表
function loadData(pageNo) {
    console.log("loadData");
    $.ajax({
        url: "/manage/account/list",
        type: 'POST',
        data: {pageNum: pageNo, pageSize: pageSize},
        success: function (data) {
            console.log(data);
            dataList = data.data.list;
            var allDataHtml = "";
            for (var i = 0; i < dataList.length; i++) {
                var cData = dataList[i];
                var cHtml = "<tr data-id=\"" + cData.id + "\" data-mobile=\"" + cData.mobile + "\" data-name=\"" + cData.name + "\">\n" +
                    "<td>" + cData.id + "</td>\n" +
                    "<td>" + cData.mobile + "</td>\n" +
                    "<td>" + cData.name + "</td>\n" +
                    "<td>\n" +
                    "<a href=\"javascript:void(0);\" onclick=\"modifyAccount(this);\">修改</a>\n" +
                    "<a href=\"javascript:delAccount('" + cData.id + "');\" class=\"red-link\">删除</a>\n" +
                    "</td>\n" +
                    "</tr>";
                allDataHtml += cHtml;
            }
            $("#resultList").html();
            $("#resultList").html(allDataHtml);

            // 处理分页
            cPage = pageNo;
            processPage(data.data);
        }
    });

}

// 创建弹窗临时数据
var temp = {
    name: '',
    mobile: '',
    password: ''
};

//创建管理员
function createAccount() {
    var arr = '<div class="createTarget">' +
        '<ul>' +
        '<li>' +
        '<span>手机号：</span>' +
        '<input type="text" id="mobile" value="' + temp.mobile + '" />' +
        '</li>' +
        '<li>' +
        '<span>姓名：</span>' +
        '<input type="text" name="name" id="name" value="' + temp.name + '" />' +
        '</li>' +
        '<li>' +
        '<span>密码：</span>' +
        '<input type="password" name="password" id="password" value="' + temp.password + '" />' +
        '</li>' +
        '</ul>' +
        '</div>';

    var config = {
        title: '创建管理员',
        body: arr,
        btn: [
            {
                title: '确定',
                className: 'button btn-ok btn-radius',
                dimiss: false,
                callback: function(){
                    return checkQualified();
                }
            },
            {
                title: '取消',
                className: 'button btn-blue-line btn-radius',
                dimiss: false,
                callback: function(){
                    return temporary();
                }
            }
        ]
    };
    createModalDom(config);
    $('#myModal').modal('show');
}

//检查输入的内容
function checkQualified() {
    var _mobile = $("#mobile"), //手机号
        _name = $("#name"), //姓名
        _password = $("#password");  //密码

    if (_mobile.val() && _name.val() && _password.val()) { //都不能为空
        console.log("_mobile:" + _mobile.val());
        console.log("_name:" + _name.val());
        console.log("_password:" + _password.val());
        $.ajax({
            url: "/manage/account/add",
            type: "post",
            data: {mobile: _mobile.val(), name: _name.val(), password: _password.val()},
            success: function(data){
                console.log("调用成功");
                if (data.status) {
                    // 成功
                    console.log("添加成功1");
                    temp.name = '';
                    temp.mobile = '';
                    temp.password = '';
                    console.log("刷新列表");
                    loadData(cPage);
                    return true;
                } else {
                    console.log("添加失败:" + data.message);
                    return false;
                }
            },
            fail: function(data) {
                console.log("服务器内部错误");
            }
        });
        return true;
    } else {
        //return false;
        return true;
    }
}

//暂存输入的内容
function temporary() {
    var _name = $("#name"), //名称
        _mobile = $("#mobile"), //图片
        _password = $("#password");  //描述

    if (_name.val()) {
        temp.name = _name.val();
    }
    if (_mobile.val()) {
        temp.mobile = _mobile.val();
    }
    if (_password.val()) {
        temp.password = _password.val();
    }
    return true;
}

//修改管理员
function modifyAccount(account) {
    var a = $(account);
    var tr = a.parents("tr");
    var arr = '<div class="createTarget">' +
        '<ul>' +
        '<input type="hidden" id="id" value="{0}" />' +
        '<li>' +
        '<span>手机号：</span>' +
        '<input type="text" id="mobile" value="{1}" />' +
        '</li>' +
        '<li>' +
        '<span>姓名：</span>' +
        '<input type="text" name="name" id="name" value="{2}" />' +
        '</li>' +
        '</ul>' +
        '</div>';
    arr = stringFormat(arr, tr.data("id"), tr.data("mobile"), tr.data("name"));
    var config = {
        title: '修改管理员',
        body: arr,
        btn: [
            {
                title: '确定',
                className: 'button btn-ok btn-radius',
                dimiss: false,
                callback: function(){
                    return modifyAccountChecked();
                }
            },
            {
                title: '取消',
                className: 'button btn-blue-line btn-radius',
                dimiss: false,
                callback: function(){
                    return true;
                }
            }
        ]
    };
    createModalDom(config);
    $('#myModal').modal('show');
}

// 修改提交
function modifyAccountChecked() {
    var _id = $("#id"),
        _name = $("#name"), //名称
        _mobile = $("#mobile"); //手机号

    if (_mobile.val() && _name.val()) { //都不能为空
        console.log("_mobile:" + _mobile.val());
        console.log("_name:" + _name.val());
        $.ajax({
            url: "/manage/account/update",
            type: "post",
            data: {mobile: _mobile.val(), name: _name.val(), id: _id.val()},
            success: function(data){
                console.log("调用成功");
                if (data.status) {
                    // 成功
                    console.log("修改成功");
                    temp.name = '';
                    temp.mobile = '';
                    temp.password = '';
                    loadData(cPage);
                } else {
                    console.log("修改失败:" + data.message);
                }
            },
            fail: function(data) {
                console.log("服务器内部错误，请联系管理员");
            }
        });
        return false;
    } else {
        //return false;
        return true;
    }
}

// 删除管理员
function delAccount(id) {
    if(!confirm("确认删除?")) {
        return false;
    }
    $.ajax({
        url: "/manage/account/delete",
        type: "POST",
        data: {id: id},
        success: function(data) {
            if (data.status) {
                console.log("删除成功");
                loadData(cPage);
            } else {
                console.log("删除失败");
            }
        },
        fail: function(){
            console.log("网络异常或服务器内部错误，请联系管理员");
        }
    })
}


